﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using TYBase;

namespace TYRTUBase {
    public class TYTCP : ITYClient {
        public override string ParaKey() {
            return $"{IP}:{Port}";
        }
        public TYTCP(string ip, int port) {
            IP = ip;
            Port = port;
        }
        public string IP { get; set; }
        void ClearSocketBuffer(Socket socket) {
            // 设置一个适当大小的缓冲区
            byte[] buffer = new byte[1024];
            while (socket.Available > 0) // 当有数据可读时
            {
                int bytesRead = socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                if (bytesRead == 0) // 如果没有读取到数据，跳出循环
                {
                    break;
                }
            }
        }
        public int Port { get; set; }
        Socket socket = null;
        public override bool ClearInBuff() {
            if (socket == null)
                return false;
            if (socket.Connected == false) {
                return false;
            }
            ClearSocketBuffer(socket);
            return true;
        }

        public override bool Connect() {
            try {
                if (socket == null)
                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Log.I($"start connect :{ParaKey()}");
                socket.Connect(IP, Port);
                return socket.Connected;
            }
            catch (Exception ex) {
                Log.E("connect tcp fail", ex);
                return false;
            }
        }

        public override void Close() {
            try {
                if (socket == null) {
                    return;
                }

                socket.Close();
            }
            catch (Exception ex) {
                Log.E("close tcp fail", ex);
                return;
            }
        }


        public override int Write(byte[] byteData, int offset = 0, int length = -1) {
            if (socket == null || byteData == null || socket.Connected == false) {
                return -1;
            }
            if (length == -1) {
                socket.Send(byteData, offset, byteData.Length, SocketFlags.None);
                FireOnComDataSend(this, byteData);
                return byteData.Length;
            }
            else {
                socket.Send(byteData, offset, length, SocketFlags.None);
                FireOnComDataSend(this, byteData);
                return length;
            }
        }

        public override int Read(out byte[] byteData, int length = -1) {
            if (socket == null || socket.Connected == false) {
                byteData = null;
                return -1;
            }

            int readLength = socket.Available;

            if (readLength > 0) {
                byteData = new byte[readLength];
                if (length == -1) {
                    if (socket.Receive(byteData, 0, readLength, SocketFlags.None) > 0) {
                        FireOnComDataRead(this, byteData);
                        return readLength;
                    }
                }
                byteData = new byte[length];
                if (socket.Receive(byteData, 0, length, SocketFlags.None) > 0) {
                    FireOnComDataRead(this, byteData);
                    return length;
                }
            }
            byteData = null;
            return -1;
        }

        public override string GetInfo() {
            return "";
        }

        //后续处理得到当前参数的消息,例如端口等消息
        public override string GetPara() {
            return "";
        }

        public override bool IsConnected {
            get { return socket != null && socket.Connected; }
        }

    }
}
